#测试博客登录页面
import time
from selenium.webdriver.common.by import By
from common.Untils import BlogDriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class BlogLogin:
    url=""
    driver=""
    def __init__(self):
        self.url="http://8.137.19.140:9090/blog_login.html"
        self.driver=BlogDriver.driver
        self.driver.get(self.url)
    #1.成功登录的测试用例
    def LoginSucessTest(self):
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #找得到说明登录成功
        #time.sleep(1)
        #self.driver.implicitly_wait(3)
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")
        #WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "body > div.container > div.left > div > h3")))
        # 添加屏幕截图
        BlogDriver.getScreenShot()
        #返回登录页面
        #self.driver.back()
    #登陆失败的测试用例
    def LoginFailTest(self):
        try:
            #2.账号正确 密码错误
            #先清除上次输入的账号密码
            self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
            self.driver.find_element(By.CSS_SELECTOR,"#password").clear()

            self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
            self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("111111")
            self.driver.find_element(By.CSS_SELECTOR,"#submit").click()

            #10秒内显示等待直到元素出来
            alert = WebDriverWait(self.driver,20).until(EC.alert_is_present())
            alert = self.driver.switch_to.alert
            alert.accept()
            # 添加屏幕截图
            BlogDriver.getScreenShot()

            # 3.账号错误 密码正确
            # 先清除上次输入的账号密码
            self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
            self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

            self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("111111")
            self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
            self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

            # 10秒内显示等待直到元素出来
            alert = WebDriverWait(self.driver, 20).until(EC.alert_is_present())
            # 添加屏幕截图
            #BlogDriver.getScreenShot()
            alert = self.driver.switch_to.alert
            alert.accept()

            # 4.账号错误 密码错误
            # 先清除上次输入的账号密码
            self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
            self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

            self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhang")
            self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("11111")
            self.driver.find_element(By.CSS_SELECTOR, "#submit").click()


            # 10秒内显示等待直到元素出来
            alert = WebDriverWait(self.driver, 20).until(EC.alert_is_present())
            alert = self.driver.switch_to.alert
            alert.accept()
            #再清除一遍
            self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
            self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

            # 5.账号错误 密码错误
            # 先清除上次输入的账号密码
            self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
            self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

            self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("11111")
            self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("11111")
            self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

            # 10秒内显示等待直到元素出来
            alert = WebDriverWait(self.driver, 20).until(EC.alert_is_present())
            alert = self.driver.switch_to.alert
            alert.accept()
            # 再清除一遍
            self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
            self.driver.find_element(By.CSS_SELECTOR, "#password").clear()




            #self.driver.quit()

        except TimeoutError:
            print("超时")
        except Exception as e:
            print(f"测试失败，真实原因：{str(e)}")

#login=BlogLogin()
#login.LoginSucessTest()
#login.LoginFailTest()